package com.bdqn.base;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

/**
 * R 数据返回对象
 *
 * @author LILIBO
 * @since 2021-12-25
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 成功状态码 */
    public static final int CODE_SUCCESS = 0;
    /** 失败状态码 */
    public static final int CODE_FAILURE = 1;

    /** 成功消息 */
    public static final String MSG_SUCCESS = "success";
    /** 失败消息 */
    public static final String MSG_FAILURE = "failure";

    private int code;
    private String msg;
    private T data;

    /**
     * 返回成功
     *
     * @param data
     * @return
     */
    public static R success(Object data) {
        return new R(CODE_SUCCESS, MSG_SUCCESS, data);
    }

    /**
     * 返回成功
     *
     * @param msg
     * @param data
     * @return
     */
    public static R success(String msg, Object data) {
        return new R(CODE_SUCCESS, msg, data);
    }

    /**
     * 返回失败
     *
     * @return
     */
    public static R failure() {
        return new R(CODE_FAILURE, MSG_FAILURE, null);
    }

    /**
     * 返回失败
     *
     * @param msg 返回消息
     * @return
     */
    public static R failure(String msg) {
        return new R(CODE_FAILURE, msg, null);
    }

    /**
     * 返回失败
     *
     * @param data
     * @return
     */
    public static R failure(Object data) {
        return new R(CODE_FAILURE, MSG_FAILURE, data);
    }

    /**
     * 返回失败
     *
     * @param msg 返回消息
     * @param data 返回数据
     * @return
     */
    public static R failure(String msg, Object data) {
        return new R(CODE_FAILURE, msg, data);
    }

}
